/**
 * @description 账号增删改查 api 路由
 * @author LI
 */

const router = require('koa-router')()
const {
  login,
  addAccount,
  getAccountAll,
  updatePwd,
  deleteAccount,
  updateLinkUser,
  getDeductionRecords
} = require('../../controller/account')

router.prefix('/jihuide/account')

// 登录
router.post('/login', async (ctx, next) => {
  const { account, pwd } = ctx.request.body
  // 接下来交给控制层
  ctx.body = await login({
    account,
    pwd
  })
})

// 添加账号信息
router.post('/addAccount', async (ctx, next) => {
  const { account, pwd, name, typecode, remark } = ctx.request.body
  // 接下来交给控制层
  ctx.body = await addAccount({
    account,
    pwd,
    name,
    typecode,
    remark
  })
})

// 查询所有账号信息
router.post('/getAccountAll', async (ctx, next) => {
  let { name, page, limit, typecode, typeLevel } = ctx.request.body
  typecode = Number(typecode)
  // 接下来交给控制层
  ctx.body = await getAccountAll({
    name,
    page,
    limit,
    typecode,
    typeLevel
  })
})

// 查询链接用户扣款记录
router.post('/getDeductionRecords', async (ctx, next) => {
  let { linkUserId, page, limit } = ctx.request.body
  linkUserId = parseInt(linkUserId)
  // 接下来交给控制层
  ctx.body = await getDeductionRecords({
    linkUserId,
    page,
    limit
  })
})

// 修改/重置密码
router.post('/updatePwd', async (ctx, next) => {
  let { id, pwd } = ctx.request.body
  id = parseInt(id)
  // 接下来交给控制层
  ctx.body = await updatePwd({
    ctx,
    id,
    pwd,
  })
})

// 修改/链接用户信息
router.post('/updateLinkUser', async (ctx, next) => {
  let { id, name, companyAddress, adPrice, balance } = ctx.request.body
  id = parseInt(id)
  adPrice = Number(adPrice)
  balance = Number(balance)
  // 接下来交给控制层
  ctx.body = await updateLinkUser({
    id,
    name,
    companyAddress,
    adPrice,
    balance,
  })
})

// 删除账号
router.post('/deleteAccount', async (ctx, next) => {
  let { id } = ctx.request.body
  id = parseInt(id)
  // 接下来交给控制层
  ctx.body = await deleteAccount(id)
})

module.exports = router
